package com.example.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.config.QRCodeConfig;
import com.example.demo.entity.QRCode;
import com.example.demo.service.QRCodeService;
import com.example.demo.util.HttpUtil;
import com.example.demo.util.RandomUtil;
import com.example.demo.vo.DataView;
import com.example.demo.vo.QRCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


/*
生成二维码
 */
@RestController
public class QRCodeController {

    @Autowired
    private QRCodeConfig qrCodeConfig;

    @Autowired
    HttpUtil httpUtil;

    @Autowired
    private QRCodeService qrCodeService;

    private final static String url = "http://cc/qrcode?codeId=";

    /**
     * 生成二维码
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/generateQRCode")
    public ResponseEntity<byte[]> generateQRCodeTo(HttpServletResponse response) throws IOException {

        String codeId = RandomUtil.random();

        BufferedImage generate = QrCodeUtil.generate(url+codeId, qrCodeConfig.qrConfig());
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write(generate,"png",byteArrayOutputStream);

        QRCode qrCode = new QRCode();
        qrCode.setIsUse(0);
        qrCode.setQrCodeId(codeId);
        qrCodeService.save(qrCode);

        return ResponseEntity.status(HttpStatus.OK)
                .contentType(MediaType.IMAGE_PNG)
                .body(byteArrayOutputStream.toByteArray());
    }

    @RequestMapping(value = "/readQRCode")
    public DataView readQRCode(@RequestBody QRCodeVo qrCodeVo){
        String qrCodeId = qrCodeVo.getQrCodeId();
        if(qrCodeId == null)
        {
            return DataView.fail(400,"二维码不存在");
        }

        DataView dataView = new DataView();
        QRCode one = qrCodeService.query().eq("qr_code_id", qrCodeId).one();
        if(one == null)
        {
            dataView.setMsg("二维码不存在");
            dataView.setCode(400);
            return dataView;
        }
        int use = one.getIsUse();
        if(use==0)
        {
            dataView.setCode(220);
            dataView.setMsg("跳转到创作界面");
            return dataView;
        }

        if(use == 1)
        {
            dataView.setMsg("跳转到展示界面");
            dataView.setCode(200);
            return dataView;
        }
        return DataView.success(0);
    }

}
